package com.cema.manage.project.system.user.controller;

import com.cema.manage.common.utils.StringUtils;
import com.cema.manage.framework.config.reasahiConfig;
import com.cema.manage.framework.shiro.web.filter.captcha.CaptchaValidateFilter;
import com.cema.manage.framework.web.controller.BaseController;
import com.cema.manage.framework.web.domain.Message;
import com.cema.manage.project.enums.RedisEnum;
import com.cema.manage.redis.RedisService;
import com.google.code.kaptcha.Constants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * 登录验证
 *
 * @author reasahi
 */
@Controller
public class LoginController extends BaseController {
    @Resource
    private RedisService redisService;
    /** 系统基础配置 */
    @Autowired
    private com.cema.manage.framework.config.reasahiConfig reasahiConfig;

    @GetMapping("/login")
    public String login(Model model) {
        model.addAttribute("captchaEbabled", true);
        model.addAttribute("captchaType", "char");
        model.addAttribute("appName", redisService.get(RedisEnum.APP_NAME.getKey()));
        model.addAttribute("copyrightYear", reasahiConfig.getCopyrightYear());
        return "login";
    }

    @GetMapping("/page")
    public String page(Model model) {
        model.addAttribute("downloadLink", redisService.get(RedisEnum.DOWNLOAD_LINK.getKey()));
        model.addAttribute("appName", redisService.get(RedisEnum.APP_NAME.getKey()));
        return "page";
    }

    @PostMapping("/login")
    @ResponseBody
    public Message ajaxLogin(String username, String password, Boolean rememberMe, HttpServletRequest request) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        String validateCode = request.getParameter("validateCode");
        HttpSession session = request.getSession();
        Object obj = session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        String code = String.valueOf(obj != null ? obj : "");
        if (!(org.apache.commons.lang3.StringUtils.isNotBlank(validateCode) && validateCode.equalsIgnoreCase(code))) {
            return Message.error("验证码错误");
        }
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return Message.success();
        } catch (AuthenticationException e) {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return Message.error(msg);
        }
    }

    @GetMapping("/unauth")
    public String unauth() {
        return "error/unauth";
    }
}
